<?php
namespace Home\Model;
use Think\Model;
class QuotationModel extends Model {
	public $table='zjy_quotation';
	public $page; //当前页
	public $perpage; //每页显示
	public $quotation_id; //ID
	public $pc_id; //分类ID
	public $post; //提交的信息 数组
	public $ids; //IDs 主要用于删除
	public $sorts; //IDs 主要用于删除
	public $keyword; //关键词
	public $pc_ids; //获取分类ID下的子分类

	public function get_quotation_list()
	{
		$page=$this->page;
		$perpage=$this->perpage;
		//搜索
		$where='where 1 ';
		/*if($this->keyword){
			$where.="and `quotation_sn` LIKE '%".$this->keyword."%'";
		}*/
		/*
		if($this->pc_id){
			$pc_ids=$this->get_sub_pc_id($this->pc_id);
			$pc_ids=mb_strcut($pc_ids,1); //裁掉第一个，
			$where.="and c.pc_id in(".$pc_ids.")";
		}*/
		$start=($page-1)*$perpage;
		$Model=new Model();
		$Member=M("Member");
		$Area=M("Area");
		$sql="select * from ".$this->table." ".$where." order by quotation_sort asc,quotation_id desc limit $start,$perpage";
		$list=$Model->query($sql);
		//print_r($list);exit;
		foreach($list as $k=>$v){
			$member_info=$Member->where('member_id='.$v['member_id'])->find();
			$list[$k]['member_company']=$member_info['member_company'];
			$list[$k]['member_main_sell']=$member_info['member_main_sell'];
			$list[$k]['quotation_add_time']=$this->time_to_str($v['quotation_add_time']);
			$list[$k]['member_area_name']=$Area->where('area_id='.$member_info['member_area_id'])->getField('area_name');
		}

		return $list;
	}
	//将时间戳转换成描述词
	private function time_to_str($time) {
		$time = (int) substr($time, 0, 10);
		$int = time() - $time;
		$str = '';
		if ($int <= 2){
			$str = sprintf('刚刚', $int);
		}elseif ($int < 60){
			$str = sprintf('%d秒前', $int);
		}elseif ($int < 3600){
			$str = sprintf('%d分钟前', floor($int / 60));
		}elseif ($int < 86400){
			$str = sprintf('%d小时前', floor($int / 3600));
		}elseif ($int < 2592000){
			$str = sprintf('%d天前', floor($int / 86400));
		}else{
			$str = date('Y-m-d H:i:s', $time);
		}
		return $str;
	}
	//获得总数
	public function get_quotation_total()
	{
		//搜索
		$where='where 1 ';
		/*if($this->keyword){
			$where.="and `quotation_sn` LIKE '%".$this->keyword."%'";
		}*/
		/*if($this->pc_id){
			$pc_ids=$this->get_sub_pc_id($this->pc_id);
			$pc_ids=mb_strcut($pc_ids,1); //裁掉第一个，
			$where.="and pc_id in(".$pc_ids.")";
		}*/
		$Model=new Model();
		$sql="select count(quotation_id) as total from ".$this->table." ".$where;
		$query=$Model->query($sql);
		return $query[0]['total'];
	}
	//获取信息
	public function get_quotation_info()
	{
		$quotation_id=intval($this->quotation_id);
		$Model=M("Quotation");
		$Member=M("Member");
		$info=$Model->where('quotation_id='.$quotation_id)->find();
		//print_r($info);exit;
		$member_info=$Member->where('member_id='.$info['member_id'])->find();
		//$info['member_truename']=$Member->where('member_id='.$info['member_id'])->getField('member_truename');
		$info['member_company']=$member_info['member_company'];
		$info['member_main_sell']=$member_info['member_main_sell'];
		//print_r($info);exit;
		return $info;
	}
	//提交
	public function quotation_add_post(){
		$Model=M("Quotation");
		$data=$this->post;
		if($Model->add($data)){
			return true;
		}else{
			return false;
		}
	}


	/*public function get_quotation_list()
	{
		$page=$this->page;
		$perpage=$this->perpage;
		//搜索
		$where='where 1 ';
		if($this->keyword){
			$where.="and `quotation_sn` LIKE '%".$this->keyword."%'";
		}
		/*
		if($this->pc_id){
			$pc_ids=$this->get_sub_pc_id($this->pc_id);
			$pc_ids=mb_strcut($pc_ids,1); //裁掉第一个，
			$where.="and c.pc_id in(".$pc_ids.")";
		}*
		$start=($page-1)*$perpage;
		$Model=new Model();
		$Member=M("Member");
		$sql="select * from ".$this->table." ".$where." order by quotation_sort asc,quotation_id desc limit $start,$perpage";
		$list=$Model->query($sql);
		foreach($list as $k=>$v){
			$list[$k]['member_truename']=$Member->where('member_id='.$v['member_id'])->getField('member_truename');
			$list[$k]['member_company']=$Member->where('member_id='.$v['member_id'])->getField('member_company');
		}
		//print_r($list);exit;
		return $list;
	}
	//获得总数
	public function get_quotation_total()
	{
		//搜索
		$where='where 1 ';
		if($this->keyword){
			$where.="and `quotation_sn` LIKE '%".$this->keyword."%'";
		}
		/*if($this->pc_id){
			$pc_ids=$this->get_sub_pc_id($this->pc_id);
			$pc_ids=mb_strcut($pc_ids,1); //裁掉第一个，
			$where.="and pc_id in(".$pc_ids.")";
		}*
		$Model=new Model();
		$sql="select count(quotation_id) as total from ".$this->table." ".$where;
		$query=$Model->query($sql);
		return $query[0]['total'];
	}
	//获取信息
	public function get_quotation_info()
	{
		$quotation_id=intval($this->quotation_id);
		$Model=M("Quotation");
		$Member=M("Member");
		$info=$Model->where('quotation_id='.$quotation_id)->find();
		//print_r($info);exit;
		$info['member_truename']=$Member->where('member_id='.$info['member_id'])->getField('member_truename');
		$info['member_company']=$Member->where('member_id='.$info['member_id'])->getField('member_company');
		//print_r($info);exit;
		return $info;
	}
	//编辑提交
	public function quotation_edit_post()
	{
		$Model=M("Quotation");
		$where['quotation_id'] = $this->post['quotation_id'];
		$data=$this->post;
		$result=$Model->where($where)->save($data);
		if($result === false) {
			return false;
		}else{
			return true;
		}
	}
	//删除
	public function delete_quotation()
	{
		$ids=$this->ids;
		$Model=M("Quotation");
		foreach($ids as $v){
			$condition['quotation_id']=$v;
			if(!$Model->where($condition)->delete()){
				return false;
			}
		}
		return true;
	}
	//批量更新排序
	public function update_quotation_sort()
	{
		$ids=$this->ids;
		$sorts=$this->sorts;
		$Model=M("Quotation");
		foreach($ids as $k=>$v){
			$data['quotation_sort']=$sorts[$k];
			$condition['quotation_id']=$v;
			$result=$Model->where($condition)->save($data);
			if($result === false) {
				return false;
			}
		}
		return true;
	}*/

}